package com.manor.paradise.activity

import android.graphics.Color
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.bumptech.glide.Glide
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.manor.common.activity.AbsActivity
import com.manor.common.http.JHttpCallback
import com.manor.common.utils.ClickUtil
import com.manor.common.utils.GsonUtil
import com.manor.common.utils.KLog
import com.manor.common.utils.ToastUtil
import com.manor.paradise.R
import com.manor.paradise.adapter.AuraValueAdapter
import com.manor.paradise.bean.AuraValueBean
import com.manor.paradise.bean.ReikInfoBean
import com.manor.paradise.http.ElvesHttpConst
import com.manor.paradise.http.ElvesHttpUtil
import kotlinx.android.synthetic.main.activity_aura_value.*
import java.math.BigDecimal

/**
 * 我的活跃度
 */
class AuraValueActivity : AbsActivity() {
    private var isReiki: Boolean = true
    internal lateinit var mAdapter: AuraValueAdapter

    var pageNum = 1
    override fun getLayoutId(): Int {
        return R.layout.activity_aura_value
    }


    override fun main() {
        super.main()
        im_back.setOnClickListener { finish() }
        rcv_list.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
        ivQuest.setOnClickListener {
            ExplainActivity.forward(this, ExplainActivity.AURA_VALUE)
        }
        tvOutflow.setOnClickListener {
            if (ClickUtil.canClick()){
//                setIsCheck(tvOutflow,true)
//                setIsCheck(tvReiki,false)
                isReiki = false
                setData(true)
            }
        }
        tvReiki.setOnClickListener {
//            if (ClickUtil.canClick()){
//                setIsCheck(tvOutflow,false)
//                setIsCheck(tvReiki,true)
//                isReiki = true
//                setData(true)
//            }
        }

//        mAdapter = NpcShopAdapter(R.layout.item_aura_value, arr)
//        rcv_list.setAdapter(mAdapter)
        refreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onLoadMore(refreshLayout: RefreshLayout) {
                pageNum++
                setData()
            }

            override fun onRefresh(refreshLayout: RefreshLayout) {
                pageNum = 1
                setData()
            }
        })
        setData()
        initMode()
    }

    private fun initMode() {
        ElvesHttpUtil.getGetInfo(object : JHttpCallback() {
            override fun onSuccess(code: Int, msg: String, info: String) {
                if (code != 200) {
                    ToastUtil.show(msg)
                    return
                }
                var reikindexBean: ReikInfoBean = GsonUtil.gsonToBean<ReikInfoBean>(info, ReikInfoBean::class.java)
                KLog.e("json", reikindexBean)

                tv_zlq.text = BigDecimal(reikindexBean.baseAnima).add(BigDecimal(reikindexBean.additionAnima)).toString()
                tv_jclq.text = reikindexBean.baseAnima.toString()
                tv_jclqs.text = reikindexBean.additionAnima.toString()
                tv_loss.text = reikindexBean.lossAnima.toString()
            }

            override fun onError() {
                super.onError()
            }
        })
    }

    fun setData(isRefresh: Boolean = false) {
        if (isRefresh)
            pageNum = 1
        if (isReiki){
            ElvesHttpUtil.getdetailsList(pageNum, httpCallBack)
        } else {
            ElvesHttpUtil.getLossList(pageNum, httpCallBack)
        }
    }

    private val httpCallBack = object : JHttpCallback() {
        override fun onSuccess(code: Int, msg: String, info: String) {
            if (code != 200) {
                ToastUtil.show(msg)
                return
            }
            var auraValueBean: AuraValueBean = GsonUtil.gsonToBean<AuraValueBean>(info, AuraValueBean::class.java)
            KLog.e("json", auraValueBean)
            if (pageNum == 1) {
                mAdapter = AuraValueAdapter(R.layout.item_aura_value, auraValueBean.records)
                rcv_list.adapter = mAdapter
                refreshLayout.finishRefresh()
            } else {
                mAdapter.addData(auraValueBean.records)
                refreshLayout.finishLoadMore()
            }

            if (mAdapter?.data == null || mAdapter.data.isEmpty()) {
                mAdapter?.setEmptyView(R.layout.view_empty_default)
            }

        }

        override fun onError() {
            super.onError()
        }
    }

    override fun onDestroy() {
        Glide.get(this).clearMemory()
        ElvesHttpUtil.cancel(ElvesHttpConst.POST_OPEN_USER_ANIMA_LIST)

        ElvesHttpUtil.cancel(ElvesHttpConst.POST_OPEN_GET_INFO)
        super.onDestroy()
    }


    private fun setIsCheck(tv: TextView, boo: Boolean) {
        if (boo) {
            tv.textSize = 18f
            tv.setTextColor(Color.parseColor("#FF5400"))
            tv.paint?.isFakeBoldText = true
        } else {
            tv.textSize = 16f
            tv.setTextColor(Color.parseColor("#312222"))
            tv.paint?.isFakeBoldText = false
        }
    }

}
